#!/bin/sh
# PCP QA Test No. 953
# non-valgrind version of 4751
#
# Copyright (c) 2017 Ken McDonell.  All Rights Reserved.
#
# check-group-exclude: iostat

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

_get_libpcp_config
$multi_threaded || _notrun "No libpcp threading support"

status=0	# success is the default!
$sudo rm -f /tmp/func?.out
trap "rm -f $tmp.* /tmp/func?.out; exit \$status" 0 1 2 3 15

# collect stderr as we go
exec 2>> $seq_full

openfiles=`ulimit -n`
if [ "$openfiles" -lt 1024 ]
then
    ulimit -n 1024
fi

_filter_ncpu()
{
  tee -a $seq_full | sed -e 's, [0-9]*$, NUMBER,'
}

# depending on network setup, there is a race between ...
# 192.0.2.10 -110 (Connection timed out)
# and
# 192.0.2.10 -101 (Network is unreachable)
# and
# 192.0.2.10 -113 (No route to host)
# and
# 192.0.2.10 -111 (Connection refused)
# and
# 192.0.2.22 -60 (Operation timed out, Mac OS X)
# and
# 192.0.2.22 -51 (Network is unreachable, Mac OS X)
# and
# 192.0.2.22 -65 (No route to host, Mac OS X)
# and
# 192.0.2.22 -61 (Connection refused. *BSD)
# so treat 'em as equivalent here ...
#
_filter_connect_fail()
{
    sed \
	-e '/^192\..*-110$/s/-110/NETERROR/' \
	-e '/^192\..*-101$/s/-101/NETERROR/' \
	-e '/^192\..*-113$/s/-113/NETERROR/' \
	-e '/^192\..*-111$/s/-111/NETERROR/' \
	-e '/^192\..*-60$/s/-60/NETERROR/' \
	-e '/^192\..*-51$/s/-51/NETERROR/' \
	-e '/^192\..*-65$/s/-65/NETERROR/' \
	-e '/^192\..*-61$/s/-61/NETERROR/' \
    # end
}

# real QA test starts here
echo "=== Only one context ===" | tee -a $seq_full
src/multithread10 localhost \
| _filter_ncpu 

echo | tee -a $seq_full
echo "=== A couple of localhosts ===" | tee -a $seq_full
src/multithread10 localhost 127.0.0.1 localhost localhost local: local: localhost \
| _filter_ncpu \
| _filter_connect_fail

echo | tee -a $seq_full
echo "=== A couple of unreachable hosts ===" | tee -a $seq_full
ips=""
for i in 1 2 3 4; do
    for j in 0 1 2 3 4 5 6 7 8 9; do
        ips="$ips 192.0.2.${i}${j}"
    done
done
src/multithread10 $ips \
| _filter_ncpu \
| _filter_connect_fail

echo | tee -a $seq_full
echo "=== A mixture of archives and hosts and unreachable hosts ===" | tee -a $seq_full
ips="localhost local: localhost local: localhost local: 127.0.0.1 127.0.0.1 "
for i in 1 2 1 2 ; do
    for j in 0 1 2 3 4 5 6 7 8 9; do
        ips="$ips 192.0.2.${i}${j}"
    done
done
# a snapshot of archives as of our incept date, some of them repeated
archives="
./archives/tzchange-10-a.meta
./archives/tzchange-10-b.meta
./archives/tzchange-11-a.meta
./archives/tzchange-11-b.meta
./archives/tzchange-12-a.meta
./archives/tzchange-12-b.meta
./archives/pcp-vmstat.meta
./archives/pcp-atop-log.meta
./archives/pmiostat_mark.meta
./archives/proc.meta
./archives/procpid-encode2.meta
./archives/procpid-encode.meta
./archives/eventrec-old.meta
./archives/fcsw_indom.meta
./archives/foo+.meta
./archives/gap2.meta
./archives/gap.meta
./archives/gmt-boring.meta
./archives/instant-1.meta
./archives/instant-base.meta
./archives/interpmark.meta
./archives/kenj-pc-1.meta
./archives/kenj-pc-2.meta
./archives/kenj-pc-diskstat.meta
./archives/20041125.meta
./archives/20071023-08-06-36.meta
./archives/20101004-trunc.meta
./archives/20130706.meta
./archives/20150105.17.57-00.meta
./archives/20150105.17.57.meta
"
src/multithread10 $archives $ips $archives $ips \
| _filter_ncpu \
| _filter_connect_fail

# success, all done
exit
